﻿/******************************************************************************
 * Copyright (c) 2011, S. Kyle Korndoerfer
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *  * Redistributions of source code must retain the above copyright notice, 
 *      this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the 
 *      documentation and/or other materials provided with the distribution.
 *  * Neither the name of KyKo Software nor the names of its contributors may 
 *      be used to endorse or promote products derived from this software 
 *      without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE. 
 *****************************************************************************/

using System.Resources;
using System.Reflection;
using System.Globalization;

namespace wsdEditor.Localization
{
    /// <summary>
    ///     Provides simplified access to localized strings.
    /// </summary>
    public abstract class Strings
    {
        #region Fields

        /// <summary>Resource Manager instance.</summary>
        private static ResourceManager mResources = new ResourceManager(
                typeof( Strings ).Namespace + ".Strings",
                Assembly.GetAssembly( typeof( Strings ) ) );

        #endregion

        #region Public Static Methods

        /// <summary>
        ///     Looks up the specified key in the resource bundle and returns
        ///     the localized value.
        /// </summary>
        /// <param name="key">The key to convert.</param>
        /// <returns>The localized text associated with the key.</returns>
        public static string Translate( string key )
        {
            return Translate( key, true );
        }// Translate( key )

        /// <summary>
        ///     Looks up the specified key in the resource bundle and returns
        ///     the localized value.
        /// </summary>
        /// <param name="key">The key to convert.</param>
        /// <param name="culture">The culture to convert to.</param>
        /// <returns>The localized text associated with the key.</returns>
        public static string Translate( string key, CultureInfo culture )
        {
            return Translate( key, culture, true );
        }// Translate( key, culture )

        #endregion

        #region Private Static Methods

        /// <summary>
        ///     Looks up the specified key in the resource bundle and returns
        ///     the localized value. Optionally returns the key if the value
        ///     wasn't found.
        /// </summary>
        /// <remarks>
        ///     If <c>fallback = false</c> and the key wasn't found in the
        ///     resource bundle, <c>null</c> will be returned.
        /// </remarks>
        /// <param name="key">The key to convert.</param>
        /// <param name="fallback">Whether or not to return the key if it
        ///     couldn't be found in resource bundle.</param>
        /// <returns>The localized text associated with the key.</returns>
        private static string Translate( string key, bool fallback )
        {
            var value = mResources.GetString( key );

            return ( value == null && fallback ) ? key : value;
        }// Translate( key, fallback )

        /// <summary>
        ///     Looks up the specified key in the resource bundle and returns
        ///     the localized value. Optionally returns the key if the value
        ///     wasn't found.
        /// </summary>
        /// <remarks>
        ///     If <c>fallback = false</c> and the key wasn't found in the
        ///     resource bundle, <c>null</c> will be returned.
        /// </remarks>
        /// <param name="key">The key to convert.</param>
        /// <param name="culture">The culture to convert to.</param>
        /// <param name="fallback">Whether or not to return the key if it
        ///     couldn't be found in resource file.</param>
        /// <returns>The localized text associated with the key.</returns>
        private static string Translate( string key, CultureInfo culture, bool fallback )
        {
            var value = mResources.GetString( key, culture );

            return ( value == null && fallback ) ? key : value;
        }// Translate( key, culture, fallback )

        #endregion

    }// Strings

}// wsdEditor.Resources
